//service------------------------------------
function vd(obj) {
    var out = "";
    if(obj && typeof(obj) == "object"){
        for (var i in obj) {
            out += i + ": " + obj[i] + "\n";
        }
    } else {
        out = obj;
    }
    alert(out);
}

function isDigit(v){
    if (!isNaN(parseInt(v * 1)))
        return true;
        return false;
}

function err(message){
    alert( message );
}

//------------------------------------------
function PointStatic(x, y){
    /* Координати зберігати у сотих (0.01) від верхнього лівого кута */
    
    this.x = null;
    this.y = null;
    
    this.setX = function(percent){ 
        if (percent < 0 || percent > 1)
            throw 'PointStatic -> setX() -> Incorrect X percent !';
        this.x= percent;
    }
    this.setY = function(percent){
        if (percent < 0 || percent > 1)
            throw 'PointStatic -> setY() -> Incorrect Y percent !';
        this.y= percent;
    }
    
    this.getX = function (){
        if (this.x == null)
            throw 'PointStatic -> getX() -> x is null !'
        return this.x;
    }
    this.getY = function (){ 
        if (this.x == null)
            throw 'PointStatic -> getY() -> y is null !'
        return this.y;    
    }
    this.isValid = function(){
        if (this.x != null && this.y != null){
            return true;
        } else {
            return false;
        }
    }
    this.setX(x);
    this.setY(y);
}

function PointDinamic(line, percent){
    this.relationLine = null;
    this.percent = null;
    
    this.currentX = null;
    this.currentY = null;
    
    this.setRelationLine = function(line, isRecalc){
        if (!(line instanceof Line))
            throw 'PointDinamic -> setRelationLine() -> incorrect line !';
        this.relationLine = line;
        if (isRecalc != null && isRecalc == true)
            this.calcCurrentCoords();
        return true;
    }
    
    this.setPercent = function(percent, isRecalc){
        if (percent < 0 || percent > 1)
            throw 'PointDinamic -> setPercent() -> incorect percent !';
        this.percent = percent;
        if (isRecalc != null && isRecalc == true)
            this.calcCurrentCoords();
        return true;
    }
    
    this.setLinePercent = function(line, percent){
        this.setRelationLine(line);
        this.setPercent(percent, true);
    }
    
    this.calcCurrentCoords = function(){
        if (this.relationBezie != null &&
        this.percent != null){
            var intersectionPoint = this.relationLine.calcPointInLine(this.percent);
            if (intersectionPoint != null){
                this.currentX = intersectionPoint['x'];
                this.currentY = intersectionPoint['y'];
            }
        }
    }
    this.isValid = function(){
        if (this.currentX != null & this.currentY != null)
            return true;
        return false;
    }    
    this.getX = function(){
        if (this.currentX != null)
            return this.currentX;
        return null;
    }
    
    this.getY = function(){
        if (this.currentY != null)
            return this.currentY;
        return null;
    }
}

function Line(){
    this.points = new Array();
    this.degree = -1;
    
    this.setPoint = function(point, number){
        if ((point instanceof PointDinamic ||
        point instanceof PointStatic) &&
        (number >=0 && number <=4)){
            this.points[number] = point
            for (var i = this.degree; i <= 2; i++){
                if (this.points[i+1] == null)
                    break;
                this.degree ++;
            }
            return true;
        } else {
            return false;
        }
    }
    
    this.getDegree = function(){
        return this.degree;
    }
    
    this.isValid = function(){
        if (this.degree + 1 == this.point.length && this.degree > 0)
            return true;
        return false;
    }
    
    this.calcPointInLine = function(percent){
        switch(this.degree){
            case 1: {
                var res = Array();
                res['x'] = (1-percent) * this.points[0].getX() + 
                    percent * this.points[1].getX();
                res['y'] = (1-percent) * this.points[0].getY() + 
                    percent * this.points[1].getY();
                return res;
                break;
            }
            case 2: {
                res['x'] = match.pow(1-percent, 2) * this.points[0].getX() + 
                    (1 - percent) * percent * this.points[1].getX() +
                    match.pow(percent, 2) * this.points[2].getX();
                res['y'] = match.pow(1-percent, 2) * this.points[0].getY() + 
                    (1 - percent) * percent * this.points[1].getY() +
                    match.pow(percent, 2) * this.points[2].getY();
                return res;
                break;
            }
            case 3: {
                res['x'] = match.pow(1 - percent, 3) * this.points[0].getX() + 
                    3 * match.pow((1 - percent), 2) * percent * this.points[1].getX() +
                    3 * (1 - t) * match.pow(percent, 2) * this.points[2].getX() +
                    match(t, 3) * this.points[3].getX();
                res['y'] = match.pow(1 - percent, 3) * this.points[0].getY() + 
                    3 * match.pow((1 - percent), 2) * percent * this.points[1].getY() +
                    3 * (1 - t) * match.pow(percent, 2) * this.points[2].getY() +
                    match(t, 3) * this.points[3].getY();
                return res;
                break;
            }
            default: {
                return null;
            }
        }
    }
    
    this.calcIntersection = function(line){
        if (!(line instanceof Line)) return null;
        
    }
}

$(function(){
    p1 = new PointStatic(0, 0);
    p2 = new PointStatic(1, 1);
    p3 = new PointStatic(0, 1);
    p4 = new PointStatic(1, 0);
    
    l1 = new Line();
    l1.setPoint(p1, 0);
    l1.setPoint(p2, 1);
    l2 = new Line();
    l2.setPoint(p3, 0);
    l2.setPoint(p4, 1);
    
    vd(l2.calcPointInLine(0.153));
    
    /*t = new Intersection();
    p1 = new Point2D(-0, -0.0001);
    p2 = new Point2D(-20.345, -20);
    p3 = new Point2D(-40.345, -20);
    p4 = new Point2D(-60.456, 0);
    p5 = new Point2D(-30.456, 0);
    p6 = new Point2D(-20.54, -26);
    p7 = new Point2D(-40.544556, -40);
    p8 = new Point2D(-60.456, -40);
    var d = new Date();
    var n = d.getTime();
    var res = Intersection.intersectBezier3Bezier3(p1, p2, p3, p4, p5, p6, p7, p8);
    var d1 = new Date();
    var n2 = d1.getTime();
    alert(n2 - n);*/
    //vd(res.points.max());
});